﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Admin_AddCompany : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Roles.IsUserInRole(User.Identity.Name, "Administrator"))
        {
            Response.Redirect("~/Account/Login.aspx");
        }
        string companyId = null;
        if (Session["companyId"] != null)
        {
            companyId = Session["companyId"].ToString();
        }
        if (companyId != null && ViewState["isEditing"] == null)
        {
            Company selectedCompany = getCompany(Convert.ToInt32(companyId));
            if (selectedCompany != null)
            {
                ViewState["isEditing"] = true;
                CompanyName.Text = selectedCompany.CompanyName;
                CreationDate.Text = selectedCompany.CreationDate.ToShortDateString();
                FormList.SelectedValue = selectedCompany.OrganitionalFormId.ToString();
                SizeList.SelectedValue = selectedCompany.CompanySizeId.ToString();
                ActivityList.SelectedValue = selectedCompany.ActivityId.ToString();
            }
            AddCompany.Text = "Обновить";
        }
    }

    protected Company getCompany(int companyId)
    {
        var companies = from company in db.Companies where company.CompanyId == companyId select company;
        if (companies.Count() > 0)
        {
            return companies.First();
        }
        return null;
    }

    protected void AddCompany_Click(object sender, EventArgs e)
    {
        string companyId = null;
        if (Session["companyId"] != null)
        {
            companyId = Session["companyId"].ToString();
        }
        var comps = from comp in db.Companies where comp.CompanyName == CompanyName.Text select comp;
        if ((comps.Count() > 0) && (comps.First().CompanyId != Convert.ToInt32(companyId)))
        {
            ErrorLabel.Text = "Компания с таким названием уже есть";
            return;
        }
        if (companyId != null)
        {
            Company selectedCompany = getCompany(Convert.ToInt32(companyId));
            if (selectedCompany != null)
            {
                selectedCompany.CompanyName = CompanyName.Text;
                selectedCompany.CreationDate = DateTime.Parse(CreationDate.Text);
                selectedCompany.ActivityId = Convert.ToInt32(ActivityList.SelectedValue);
                selectedCompany.OrganitionalFormId = Convert.ToInt32(FormList.SelectedValue);
                selectedCompany.CompanySizeId = Convert.ToInt32(SizeList.SelectedValue);
                db.SubmitChanges();
                Response.Redirect("~/Admin/Companies.aspx");
            }
        }
        else
        {
            var comps2 = from comp in db.Companies where comp.CompanyName == CompanyName.Text select comp;
            if (comps2.Count() > 0)
            {
                ErrorLabel.Text = "Компания с таким названием уже есть";
                return;
            }
            Company newCompany = new Company();
            newCompany.CompanyName = CompanyName.Text;
            newCompany.CreationDate = DateTime.Parse(CreationDate.Text);
            newCompany.ActivityId = Convert.ToInt32(ActivityList.SelectedValue);
            newCompany.OrganitionalFormId = Convert.ToInt32(FormList.SelectedValue);
            newCompany.CompanySizeId = Convert.ToInt32(SizeList.SelectedValue);
            db.Companies.InsertOnSubmit(newCompany);
            db.SubmitChanges();
            Response.Redirect("~/Admin/Companies.aspx");
        }
    }
}